package com.angel.controller;

import com.angel.bean.Employee;
import com.angel.bean.Product;
import com.angel.bean.ProductType;
import com.angel.dto.ProductDto;
import com.angel.dtoutlis.ProductDtoUtil;
import com.angel.exception.GlobalExceptionHandler;
import com.angel.exception.StatusException;
import com.angel.service.OtherlogService;
import com.angel.service.ProductService;
import com.angel.service.ProductTypeService;
import com.angel.shiro.SystemAdminAuthorizingRealm;
import com.angel.utils.AdminUserUtils;
import com.angel.utils.NoUtlis;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageRowBounds;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.IllegalFormatCodePointException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/productController")
public class ProductController {

    @Resource
    private ProductService productService;
    @Resource
    private ProductTypeService productTypeService;
    @Autowired
    private OtherlogService otherlogService;


    /***
     *  更新商品信息
     * @param product
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping("/updateProduct")
    @ResponseBody
    public Object updateProduct(Product product) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        int result = productService.update(product);
        if (result != 0) {
            map.put("result", "success");
        }
        return map;
    }

    /***
     * 删除商品信息
     * @param product
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping("/deleteProduct")
    @ResponseBody
    public Object deleteProduct(Product product) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        int result = productService.delete(product);
        if (result != 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 添加商品
     *
     * @return 重定向所有员工
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequiresPermissions("user")
    @RequestMapping("/addProduct")
    @ResponseBody
    public Object addProduct(Product product) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        Product pro = new Product();
        Product res = null;
        String productNo;
        SystemAdminAuthorizingRealm.Principal principal = AdminUserUtils.getPrincipal(); // 当前登录员工的分店id
        int branchesId = principal.getEmployee().getBranchesId();
        do {
            productNo = NoUtlis.getProductNo(); // 生成商品编号
            pro.setBranId(branchesId);
            pro.setProductNo(productNo);
            res = productService.findOne(pro);
        } while (res != null);

        product.setBranId(branchesId);
        product.setProductNo(productNo);
        product.setState(1);
        int result = productService.insert(product);
        if (result != 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 查询单个产品
     *
     * @return
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequiresPermissions("user")
    @RequestMapping("/findOneProduct")
    @ResponseBody
    public Object findOneProduct(Product product) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        System.out.println("------------------------------------" + product.getProductId());
        Product pro = productService.findOne(product);
        if (pro != null) {
            map.put("pro", pro);
        }
        return map;
    }

    /**
     * 查询产品类型
     *
     * @return
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */

    @RequiresPermissions("user")
    @RequestMapping("/findProductType")
    @ResponseBody
    public Object findProductType(ProductType productType) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        productType.setState(1);
        List<ProductType> proTypeList = productTypeService.findProductType(productType);

        map.put("proTypeList", proTypeList);
        return map;
    }

    /**
     * 查询所有商品信息
     *
     * @return
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequiresPermissions("user")
    @RequestMapping("/findProduct")
    @ExceptionHandler(value = StatusException.class)
    public String findProduct(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {
        //将前端传回的参数改为String类型，然后进行数据类型转换，如果出现问题，则进行异常处理，异常可以是空处理
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        // 当前登录员工的分店id
        SystemAdminAuthorizingRealm.Principal principal = AdminUserUtils.getPrincipal();
        PageHelper.startPage(pageNo, 5);
        Employee employee = principal.getEmployee();
        Product product = new Product();
        if (employee.getEmTypeId() != 1){
            product.setBranId(employee.getBranchesId());
        }
        product.setState(1);
        List<Product> productList = productService.findProduct(product);
        ProductType productType = new ProductType();
        productType.setState(1);
        List<ProductType> productTypeList = productTypeService.findProductType(productType);

        PageInfo pageInfo = new PageInfo(productList);

        List<ProductDto> productDtoList = ProductDtoUtil.getProductDto(pageInfo.getList(), productTypeList);

        pageInfo.setList(productDtoList);

        if (pageInfo != null) {
            model.addAttribute("pageInfo", pageInfo);
        }
        return "productEntry";
    }


    /**
     * 模糊查询
     *
     * @param pn
     * @param model
     * @return
     */
    @RequestMapping("/searchProduct")
    @ExceptionHandler(value = StatusException.class)
    public String searchProduct(@RequestParam(value = "pn", defaultValue = "1") String pn,
                                @RequestParam(value = "type") String type,
                                @RequestParam(value = "content") String content,
                                Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        Employee et = AdminUserUtils.getPrincipal().getEmployee();
        content = content.trim().replaceAll(" ", "");
        PageHelper.startPage(pageNo, 5);
        Product product;
        product = new Product();
        if(et.getEmTypeId() != 1){
            product.setBranId(et.getBranchesId());
        }
        List<Product> productList = null;
        switch (type) {
            case "productNo":
                // 编号
                product.setProductNo(content);
                productList = productService.searchProduct(product);
                break;
            case "proEffect":
                // 功效
                product.setProEffect(content);
                productList = productService.searchProduct(product);
                break;
            case "proInstruct":
                // 说明
                product.setProInstruct(content);
                productList = productService.searchProduct(product);
                break;
            default:
                break;
        }
        PageInfo pageInfo = new PageInfo(productList);
        ProductType productType = new ProductType();
        productType.setState(1);
        List<ProductType> productTypeList = productTypeService.findProductType(productType);
        List<ProductDto> dtoList = ProductDtoUtil.getProductDto(pageInfo.getList(), productTypeList);
        pageInfo.setList(dtoList);

        model.addAttribute("type", type);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("content", content);
        return "productEntry2";
    }

}

